package com.blogSystem.type.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.blogSystem.type.dao.LikeMapper;
import com.blogSystem.type.dao.TypeMapper;
import com.blogSystem.type.pojo.Like;
import com.blogSystem.type.pojo.Type;
import com.blogSystem.type.python.RunPython;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TypeService {

    @Autowired
    private TypeMapper typeMapper;
    @Autowired
    private RunPython runPython;
    @Autowired
    private LikeMapper likeMapper;

    public Type getType(String text){

        try {
            String str=runPython.getType(text);
            Type type1=typeMapper.selectByType(str);
            return type1;

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void insert(Like like){
        if (select(like)==null) {

            like.setCount(1);
            like.setLately(new Date());
            likeMapper.insert(like);
        }
    }

    public void delete(Like like){

        likeMapper.selectById(like);

        }
        public Like select(Like like){

        return likeMapper.selectById(like);
        }
        public List<String> matchingList(String user_id){
        List<String> jsons=new ArrayList<String>();
           List<Like> likes= likeMapper.selectByUserId(user_id);
           for(Like like:likes){
                String json= JSON.toJSONString(like);
                jsons.add(json);

           }
            return jsons;
        }
        public void record(Like like){
            if (select(like)==null) {

                like.setCount(1);
                like.setLately(new Date());
                likeMapper.insert(like);
            }
            else {


                Like select_like = likeMapper.selectById(like);
                select_like.setCount(select_like.getCount()+1);
                select_like.setLately(new Date());
                likeMapper.update(select_like);
            }

    }

}
